package com.wjb.xietong.server.notify;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.wjb.xietong.server.BaseResponseDataParse;
import com.wjb.xietong.server.HeaderInfoResponse;
import com.wjb.xietong.util.IDs;
import com.wjb.xietong.util.LogD;

public class GetNotifyResponse extends BaseResponseDataParse {

	private static GetNotifyResponse instance = null;
	private HeaderInfoResponse headerInfo = null;
	private List<NotifyInfo> notifyInfoList = null;

	private GetNotifyResponse() {

	}

	public static synchronized GetNotifyResponse instance() {
		if (null == instance) {
			instance = new GetNotifyResponse();
		}
		return instance;
	}

	@Override
	public void initData() {
		setHeaderInfo(null);
		setNotifyInfoList(null);
	}

	@Override
	public boolean parseJsonObj(JSONObject response) {
		initData();

		if (null == response) {
			LogD.output("Exception: response is null");
			return false;
		}

		if (!checkRespCode(response)) {
			headerInfo = new HeaderInfoResponse();
			headerInfo.parseJsonObj(response);
			setHeaderInfo(headerInfo);
			return false;
		}

		if (response.has(IDs.WORK_SYS_NOTIFY_INFO_MAP_LIST)) {
			JSONArray array = response
					.optJSONArray(IDs.WORK_SYS_NOTIFY_INFO_MAP_LIST);
			if (null == array) {
				LogD.output("Warning: No Data!");
				return true;
			}

			notifyInfoList = new ArrayList<GetNotifyResponse.NotifyInfo>();
			for (int i = 0; i < array.length(); i++) {
				JSONObject jsonObj = array.optJSONObject(i);
				if (null == jsonObj) {
					continue;
				}

				NotifyInfo notifyInfo = new NotifyInfo();
				if (jsonObj.has(IDs.TASK_ID)) {
					notifyInfo.setTaskId(jsonObj.optLong(IDs.TASK_ID));
				}
				if (jsonObj.has(IDs.GMT_CREATE1)) {
					notifyInfo.setGmtCreate(jsonObj.optString(IDs.GMT_CREATE1));
				}
				if (jsonObj.has(IDs.NOTIFY_ID)) {
					notifyInfo.setNotifyId(jsonObj.optLong(IDs.NOTIFY_ID));
				}
				if (jsonObj.has(IDs.COM_ID)) {
					notifyInfo.setCompanyId(jsonObj.optLong(IDs.COM_ID));
				}
				if (jsonObj.has(IDs.PROJECT_ID)) {
					notifyInfo.setProjectId(jsonObj.optLong(IDs.PROJECT_ID));
				}
				if (jsonObj.has(IDs.NOTIFY_INFO)) {
					notifyInfo
							.setNotifyInfo(jsonObj.optString(IDs.NOTIFY_INFO));
				}
				notifyInfoList.add(notifyInfo);
			}
			setNotifyInfoList(notifyInfoList);
		}
		return true;
	}

	public static class NotifyInfo {
		private long taskId = 0l;
		private String gmtCreate = null;// 10-15\n09:53
		private long notifyId = 0l;
		private long companyId = 0l;
		private long projectId = 0l;
		private String notifyInfo = null;

		public long getTaskId() {
			return taskId;
		}

		public void setTaskId(long taskId) {
			this.taskId = taskId;
		}

		public String getGmtCreate() {
			return gmtCreate;
		}

		public void setGmtCreate(String gmtCreate) {
			this.gmtCreate = gmtCreate;
		}

		public long getNotifyId() {
			return notifyId;
		}

		public void setNotifyId(long notifyId) {
			this.notifyId = notifyId;
		}

		public long getCompanyId() {
			return companyId;
		}

		public void setCompanyId(long companyId) {
			this.companyId = companyId;
		}

		public long getProjectId() {
			return projectId;
		}

		public void setProjectId(long projectId) {
			this.projectId = projectId;
		}

		public String getNotifyInfo() {
			return notifyInfo;
		}

		public void setNotifyInfo(String notifyInfo) {
			this.notifyInfo = notifyInfo;
		}
	}

	public HeaderInfoResponse getHeaderInfo() {
		return headerInfo;
	}

	public void setHeaderInfo(HeaderInfoResponse headerInfo) {
		this.headerInfo = headerInfo;
	}

	public List<NotifyInfo> getNotifyInfoList() {
		return notifyInfoList;
	}

	public void setNotifyInfoList(List<NotifyInfo> notifyInfoList) {
		this.notifyInfoList = notifyInfoList;
	}

}
